package com.iretailer.report.web.business;

import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.RePhysicalZones;
import com.iretailer.report.model.PhysicalZoneMall;
import com.iretailer.report.model.PhysicalZones;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 13:53
 * @Description:
 */
@RestController
public class PhysicalController extends BaseAction {


    @RequestMapping("/business/SelectPhysicalManager.action")
    public Map<String, Object> select(@RequestBody RePhysicalZones repz) {
        List<PhysicalZoneMall> find = physicalzonesService.selectPhysicalZoneZoneType(1, repz.getMallIds());
        return RMap.ok(find);
    }

    @RequestMapping("/business/selectPZZTByMallIdZoneTypeEnable.action")
    public Map<String, Object> selectPZZTByMallIdZoneTypeEnable(@RequestBody RePhysicalZones repz) {
        List<PhysicalZoneMall> find = physicalzonesService.selectPZZTByMallIdZoneTypeEnable(1, repz.getMallIds(), repz.getZoneTypes());
        return RMap.ok(find);
    }


    @RequestMapping("/business/gridPhysicalZone.action")
    public Map<String, Object> grid(@RequestBody RePhysicalZones repz, HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(repz, QueryOption.class);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult result = physicalzonesService.grid(userId, repz.getEnabled(), repz.getMallName(), repz.getName(), repz.getZoneTypeName(), queryOption);
        return RMap.ok(result);
    }


    @RequestMapping("/business/saveOrUpdatePhysical.action")
    public Map<String, Object> saveOrUpdatePhysical(@RequestBody RePhysicalZones rpz,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        PhysicalZones physicalZones = modelMapper.map(rpz, PhysicalZones.class);
        boolean result = physicalzonesService.saveOrUpdate(physicalZones, user.getName());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/deletPhysical.action")
    public Map<String, Object> deletPhysical(@RequestBody List<Long> ids) {
        boolean result = physicalzonesService.deletePhysicalByIds(ids);
        return RMap.resultBoolean(result);
    }
}
